#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMap>
#include <QTimer>
#include "StatusWidget.h"
#include "UCommunication/UCommunication.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);
    bool eventFilter(QObject *target, QEvent *event);

private:
    Ui::MainWindow *ui;
    QTimer *timer;
    StatusWidget *statusWidget;
    UCommunication *uCom;
    QByteArray textSDec;
    enum ComType comType;

    bool linkState;     //true:open, false:close
    bool circleState;     //true:sending, false:unsend

    bool recvAscii;     //true:ascii;false:hex
    bool recvNewline;   //true:newline
    bool recvPause;     //true:pause

    bool sendAscii;     //true:ascii;false:hex
    int  sendPeriod;    //ms

private:
    void DisplayText(const char *data, quint64 maxSize);
    int  getUdpDestIPPort(QString &ip, quint16 &port);
    void setSendOptionDisabled(bool flag);
    void setTcpClientCount();
    void startCircleSend();
    void stopCircleSend();
    void OpenUDP();
    void OpenTCPServer();
    void OpenTCPClient();
    void ChangeLinkState(bool linked);
private slots:
    int  SendText();
    void slotDataCom(const char *data, quint64 maxSize);
    void slotDataCom(QByteArray data, int size);
    void slotComingConnection(QString ip, quint16 port);
    void slotDeleteConnection(QString ip, quint16 port);
    void on_comboBoxComType_currentIndexChanged(int index);
    void on_pushButton_open_clicked();
    void on_pushSaveData_clicked();
    void on_pushRecvClear_clicked();
    void on_pushLoadData_clicked();
    void on_pushSendClear_clicked();
    void on_pushSend_clicked();
    void on_pushCloseRemote_clicked();
    void on_checkBoxRecvNewL_clicked(bool checked);
    void on_checkBoxRecvPause_clicked(bool checked);
    void on_radioButtonRecvHex_clicked(bool checked);
    void on_radioButtonRecvAscii_clicked(bool checked);
    void on_radioButtonSendAscii_clicked(bool checked);
    void on_radioButtonSendHex_clicked(bool checked);
};

#endif // MAINWINDOW_H
